<template>
	<view class="container">
		<view class="wechatapp">
			<view class="header">
				<image src="../../static/image/missing-face.png" mode="aspectFit" class="image"></image>
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			<u-button :custom-style="customStyle" :ripple="true" lang="zh_CN" @click="bindGetUserInfo()" throttle-time ="1000" shape="circle" type="primary">授权登录</u-button>
		</view>
		<view class="no-login-btn">
			<u-button lang="zh_CN" @click="onNotLogin()" throttle-time ="1000" shape="circle">暂不登录</u-button>
		</view>
	</view>
</template>

<script>
	import {  
	    mapState,  
	    mapMutations 
	} from 'vuex';
	
	export default {
		data() {
			return {
				options: {},
				customStyle: {
					backgroundColor: '#f96770'
				}
			}
		},
		onLoad(options) {
			this.options = options;
		},
		onReady(){
			uni.setNavigationBarColor({
				frontColor : '#000000',
			    backgroundColor: 'rgba(0,0,0,0)',
			})
		},
		methods: {
			onNotLogin() {
				let url = this.options.delta;
				url = decodeURIComponent(url);
				//console.log(url);
				// 如果包含?则直接拼接
				if(url.indexOf("?") != -1 ){
					url += "isCheckLogin=false";
				}else{
					url += "?isCheckLogin=false";
				}
				uni.reLaunch ({
					url : url
				});
			},
			bindGetUserInfo(){
				let that = this;
				let shareId = that.vuex_shareId;
				
				uni.getUserProfile({
					desc : "一键登录",
					lang : "zh_CN",
				    success: function (infoRes) {
						console.log('用户信息:',infoRes);
						
						uni.login({
						    success: (r) => {
								that.$u.api.sys.login(infoRes).then(res => {
									
									console.log('login接口返回值:',res);
									if(res.code === 0){
										uni.showToast({
											title: res.msg,
											icon : 'none'
										})
										return false;
									}
									
									that.$u.vuex('vuex_token', res.data.openid);
									that.$u.vuex('vuex_isRefreshUserPage', true);
									
									if(that.options != ''){
										console.log('准备跳转回去的页面:',that.options.delta);
										
										let delat = that.options.delta || '';
										delat = decodeURIComponent(delat);
										
										if(delat.indexOf("/pages/my/my") != -1){
											uni.reLaunch ({
												url : delat
											});
											return;
										}
										
										uni.navigateBack({
											delta: 1
										});
									}
								});
							},
						    fail: (err) => {
						        console.log('login fail:', err);
						    },
						});	
				    },
					fail:function(res){
						console.log('getUserProfile fail',res)
					}
				});
			}
		}
	}
</script>

<style>
	page {
	  background: #fff;
	  font-size: 32rpx;
	}
	.container {
	  padding: 0 60rpx;
	}
	
	.wechatapp {
	  padding: 80rpx 0 48rpx;
	  border-bottom: 1rpx solid #e3e3e3;
	  margin-bottom: 72rpx;
	  text-align: center;
	}
	
	.wechatapp .header .image {
	  width: 200upx;
	  height: 200upx;
	  padding: 1rpx;
	}
	
	.auth-title {
	  color: #585858;
	  font-size: 34rpx;
	  margin-bottom: 40rpx;
	}
	
	.auth-subtitle {
	  color: #888;
	  margin-bottom: 88rpx;
	  font-size: 28rpx;
	}
	
	.login-btn {
	  padding: 0 20rpx;
	}
	
	.login-btn button {
	  height: 88rpx;
	  line-height: 88rpx;
	  background: #2979ff;
	  color: #fff;
	  font-size: 30rpx;
	  border-radius: 40rpx;
	  text-align: center;
	}
	
	.no-login-btn {
	  margin-top: 20rpx;
	  padding: 0 20rpx;
	}
	
	.no-login-btn button {
	  height: 88rpx;
	  line-height: 88rpx;
	  background: #ebebeb;
	  color: #fff;
	  font-size: 30rpx;
	  border-radius: 40rpx;
	  text-align: center;
	}

</style>